package com.evidencer.java.thread;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 * 模拟票务中心售票
 * 
 * @ClassName: TicketCenter
 * @Description:票务中心
 * @author lyz lyzpoq_gmail_com
 * @date 2013/12/25 10:23:04
 */
public class TicketCenter {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// 设置高铁票务信息
		Ticket tGaotie = new Ticket("高铁");
		tGaotie.setNum(100);
		tGaotie.setTicketName("G320");
		// 设置动车票务信息
		Ticket tDongche = new Ticket("动车");
		tDongche.setNum(100);
		tDongche.setTicketName("D520");
		// 设置特快票务信息
		Ticket tTekuai = new Ticket("特快");
		tTekuai.setNum(100);
		tTekuai.setTicketName("T150");

		// 初始化高铁售票窗口
		Thread windowG001 = getTicketWindow("G001号窗口", tGaotie);
		Thread windowG002 = getTicketWindow("G002号窗口", tGaotie);
		Thread windowG003 = getTicketWindow("G003号窗口", tGaotie);
		Thread windowG004 = getTicketWindow("G004号窗口", tGaotie);
		// 初始化动车售票窗口
		Thread windowD001 = getTicketWindow("D001号窗口", tDongche);
		Thread windowD002 = getTicketWindow("D002号窗口", tDongche);
		Thread windowD003 = getTicketWindow("D003号窗口", tDongche);
		Thread windowD004 = getTicketWindow("D004号窗口", tDongche);
		// 初始化特快列车售票窗口
		Thread windowT001 = getTicketWindow("T001号窗口", tTekuai);
		Thread windowT002 = getTicketWindow("T002号窗口", tTekuai);
		Thread windowT003 = getTicketWindow("T003号窗口", tTekuai);
		Thread windowT004 = getTicketWindow("T004号窗口", tTekuai);

		// 开始高铁售票!
		windowG001.start();
		windowG002.start();
		windowG003.start();
		windowG004.start();
		// 开始动车售票!
		windowD001.start();
		windowD002.start();
		windowD003.start();
		windowD004.start();
		// 开始特快列车售票!
		windowT001.start();
		windowT002.start();
		windowT003.start();
		windowT004.start();

	}

	/**
	 * 获得已经装载票务信息的卖票窗口线程
	 * 
	 * @param ticketWindowName
	 *            表示卖票窗口名
	 * @param r
	 *            表示封装的票务信息
	 * @return 返回卖票窗口线程
	 */
	public static Thread getTicketWindow(String ticketWindowName, Runnable r) {
		Thread t = new Thread(r);
		t.setName(ticketWindowName);
		return t;
	}

}

/**
 * 票务窗口类
 * 
 * @ClassName: Ticket
 * @Description: TODO
 * @author lyz lyzpoq_gmail_com
 * @date 2013/12/25 10:26:45
 */

class Ticket implements Runnable {
	private int num;
	private String ticketName;
	public static long nanoTime=System.nanoTime();
	public Ticket(String ticketName) {
		setTicketName(ticketName);
	}

	@Override
	public void run() {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.sss");
		while (num > 0) {
			synchronized (this) {
				if (num > 0) {
					// 暂停当前线程 便于查看结果
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
					}
					// 打印当前售出的车票信息
					System.out.println("售票窗口："
							+ Thread.currentThread().getName() + "\t售出票号:"
							+ ticketName + "-" + num-- + "\t售出时间："
							+ format.format(System.currentTimeMillis())
							+(System.nanoTime()-this.nanoTime)/1000);
					this.nanoTime=System.nanoTime();
				}
			}
		}
	}


	/**
	 * 设置车票数量
	 * 
	 * @Title: setNum
	 * @Description: 车票数量
	 * @param @param num
	 * @return void
	 * @throws
	 */
	public void setNum(int num) {
		this.num = num;
	}

	/**
	 * 设置车票名称
	 * 
	 * @Title: setTicketName
	 * @Description: 车票名称
	 * @param @param ticketName
	 * @return void
	 * @throws
	 */
	public void setTicketName(String ticketName) {
		this.ticketName = ticketName;
	}
}